package kuang.blocking_queue;

import java.util.concurrent.TimeUnit;

public class ArrayBlockingQueue {

    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue();
        arrayBlockingQueue.test4();
    }

    /**
     * 抛出异常
     */
    public void test1(){
        // 队列的大小
        java.util.concurrent.ArrayBlockingQueue arrayBlockingQueue = new java.util.concurrent.ArrayBlockingQueue(3);
        System.out.println(arrayBlockingQueue.add("a"));
        System.out.println(arrayBlockingQueue.add("b"));
        System.out.println(arrayBlockingQueue.add("c"));

        System.out.println(arrayBlockingQueue.element());//查看当前队首元素
        System.out.println("==================");

        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.element());//查看当前队首元素，会报异常
    }

    /**
     * 不抛异常，有返回值
     */
    public void test2(){
        java.util.concurrent.ArrayBlockingQueue arrayBlockingQueue = new java.util.concurrent.ArrayBlockingQueue(3);

        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));
        System.out.println(arrayBlockingQueue.offer("d")); //不抛异常

        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll()); //不抛异常

        System.out.println(arrayBlockingQueue.peek()); //查看队首元素，为空时不抛异常
    }

    /**
     * 等待，阻塞（一直阻塞）
     * @throws InterruptedException
     */
    public void test3() throws InterruptedException {
        java.util.concurrent.ArrayBlockingQueue arrayBlockingQueue = new java.util.concurrent.ArrayBlockingQueue(3);

        arrayBlockingQueue.put("a");
        arrayBlockingQueue.put("b");
        arrayBlockingQueue.put("c");
        arrayBlockingQueue.put("d"); //会一直等待，一直阻塞

        arrayBlockingQueue.take();
        arrayBlockingQueue.take();
        arrayBlockingQueue.take();
        arrayBlockingQueue.take(); //没有元素，会一直等待
    }

    /**
     * 超时等待（有限时间等待）
     */
    public void test4() throws InterruptedException {
        java.util.concurrent.ArrayBlockingQueue arrayBlockingQueue = new java.util.concurrent.ArrayBlockingQueue(3);

        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));
        //超时两秒就退出
        System.out.println(arrayBlockingQueue.offer("d", 2, TimeUnit.SECONDS));
        System.out.println("==================");
        arrayBlockingQueue.poll();
        arrayBlockingQueue.poll();
        arrayBlockingQueue.poll();
        //等待两秒就退出
        arrayBlockingQueue.poll(2, TimeUnit.SECONDS);

    }
}
